package com.lagou.controller;

import com.lagou.domain.PromotionSpace;
import com.lagou.domain.ResponseResult;
import com.lagou.service.PromotionSpaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/PromotionSpace")
public class PromotionSpaceController {

    @Autowired
    private PromotionSpaceService promotionSpaceService;

    /*查询所有广告位*/
    @RequestMapping("/findAllPromotionSpace")
    public ResponseResult findAllPromotionSpace(){

        List<PromotionSpace> promotionSpaceList = promotionSpaceService.findAllPromotionSpace ();

        ResponseResult result = new ResponseResult (true, 200, "响应成功", promotionSpaceList);

        return result;
    }

    /*新增广告位*/
    @RequestMapping("/saveOrUpdatePromotionSpace")
    public ResponseResult savePromotionSpace(@RequestBody PromotionSpace promotionSpace){

        if (promotionSpace.getId () == null) {
            promotionSpaceService.savePromotionSpace (promotionSpace);

            ResponseResult result = new ResponseResult (true, 200, "新建成功", null);

            return result;
        } else {
            promotionSpaceService.updatePromotionSpace (promotionSpace);
            ResponseResult result = new ResponseResult (true, 200, "修改成功", null);

            return result;
        }

    }

    /*根据id查询广告位*/
    @RequestMapping("/findPromotionSpaceById")
    public ResponseResult findPromotionSpaceById(int id){


        PromotionSpace promotion = promotionSpaceService.findPromotionByID (id);

        ResponseResult result = new ResponseResult (true, 200, "响应成功", promotion);

        return result;
    }
}
